question=document.getElementsByClassName("block");
var keyArray = new Object();
var trukey=new Object();
function shclick(){
	var counth=0;
	var sflag=0;
	for(var j=0;j<question.length;j++)
	{
		var sh=j+1;
		var example="example"+sh;
		node=document.getElementsByName(example);
		var questi=".question"+sh;
		var tipei="#question"+sh;
		var falg=affective(node);
		if(!falg)
		{
			 if(question[j].childNodes[0].getAttribute("value"))
			 {
		 		counth++;
		 		console.log(counth);
		 		$(questi).addClass("red2");
		 		if(counth==1){
		 			document.getElementById("tipe").href=tipei;
		 		}
		 	}
		}
		else{
			keyArray[sh]=falg;
		 	$(questi).removeClass("red2");
		}	
		
	}
	var shd=0;
	for(var i=1;i<=(length(keyArray));i++)
	{
		var sh=keyArray[i];
		for(var j=0;j<mycars.length;j++){
			if(i==mycars[j]){
				if(!trukey[i]){
					trukey[i]=" ";
				}else{
					trukey[i+1]=trukey[i];
					trukey[i]=" ";
				}
				shd++;
				break;
			}
		}
		if(shd==0){
		trukey[i]=sh;
		}else{
			trukey[i+shd]=sh;
		}
	}
	var a= JSON.stringify(keyArray);
	if(counth==0)
	{
		$("#tipe").removeAttr("href");
		$.ajax({
    	type: "put",
   		url: ip+"/submit",
  		data: JSON.stringify({
  			"answer":trukey,
			"surveyId":id,
  			}),
    	async: true,
    	dataType: "json",
    	contentType: "application/json",
     	xhrFields: {withCredentials: true},
    	crossDomain:true,
    	success: function(res) {
     		if(res.msg&&res.code==-1)
     		{
     			var myurl="situation2.html?msg="+res.msg+"?id="+id;
				window.location.assign(encodeURI(myurl));
     		}else if(res.code==0){
     			if(nextid!=0)
     			{
     				window.location.href="from.html?id="+nextid;
     			}else{
     				window.location.href="situation1.html?id="+id;
     			}
     		}
    	},
    	Error: function() {
      		alert("服务器出错");
    	}
  		});
		
	}
}
function affective(node){
	var flag="";
	for(var i=0;i<node.length;i++)
	{
		if(node[i].nodeName=="TEXTAREA")
		{
			if(node[i].value)
			{
				flag=node[i].value;
				break;
			}
		}
		else{
			if(node[i].checked)
			{
				flag=node[i].value;
				break;
			}
		}
	}
	return flag;
}
function length(o){
	var count=0;
	for(var one in o){
		count++;	
	}
	return count;
}
